package com.jd.service.impl;


import com.jd.common.AjaxResult;
import com.jd.common.DataGridReq;
import com.jd.common.DataGridResult;
import com.jd.dao.DataDictionaryDao;
import com.jd.dao.GoodDao;
import com.jd.dto.GoodDto;
import com.jd.entity.Goods;
import com.jd.entity.User;
import com.jd.enums.GoodEnums;
import com.jd.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/19/019.
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodDao goodDao;
    @Autowired
    private DataDictionaryDao dataDictionaryDao;

    public DataGridResult findAllByReq(DataGridReq dataGridReq) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        User user = (User) session.getAttribute("user");

        List<GoodDto> goodList = new ArrayList<GoodDto>();
        List<Goods> list = goodDao.findAllByReq(dataGridReq);
        for (Goods good : list) {
            String typeName = dataDictionaryDao.findTypeName("good_type", good.getGoodType());
            GoodDto dto = new GoodDto();
            BeanUtils.copyProperties(good, dto);
            dto.setTypeName(typeName);
            goodList.add(dto);
        }
        int count = goodDao.count();

        DataGridResult result = new DataGridResult(count, goodList);


        return result;
    }

    public AjaxResult delete(Integer gid) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        User user = (User) session.getAttribute("user");

        int delete = goodDao.delete(gid);
        if (delete == 0) {
            return new AjaxResult(GoodEnums.DEL_ERROR);
        }
        return new AjaxResult(GoodEnums.DEL_SUC);
    }

    public AjaxResult save(Goods goods) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        User user = (User) session.getAttribute("user");

        int save = goodDao.save(goods);
        if (save == 0) {
            return new AjaxResult(GoodEnums.ADD_ERROR);
        }
        return new AjaxResult(GoodEnums.ADD_SUC);
    }

    @Transactional
    public AjaxResult update(Goods goods) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        User user = (User) session.getAttribute("user");

        int update = goodDao.update(goods);
        if (update == 0) {
            return new AjaxResult(GoodEnums.UPDATE_ERROR);
        }
        return new AjaxResult(GoodEnums.UPDATE_SUC);
    }
}
